/**
 * Created by lional on 16/10/2.
 */

jGame.ScrollView = ccui.ScrollView.extend(/** @lends jGame.ScrollView# */{

    _controller:null,
    _oneByOneCallbackListener: null,

    ctor: function () {
        this._super();
        //cc.log("jGame.ScrollView#ctor()");
    },

    setController: function(controller){
        this._controller = controller;
    },

    getController: function(){
        return this._controller;
    },

    setOneByOneCallbackName:function (touchBeganCallBackName, touchMovedCallBackName, touchEndedCallBackName, touchCancelledCallBackName, touchSwallowEnabled) {
        var widget = this;
        var onTouchBegan = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchBeganCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchBeganCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchBeganCallBackName] === "function")
                return widget[touchBeganCallBackName](touch, event);
        }
        var onTouchMoved = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchMovedCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchMovedCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchMovedCallBackName] === "function")
                return widget[touchMovedCallBackName](touch, event);
        }
        var onTouchEnded = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchEndedCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchEndedCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchEndedCallBackName] === "function")
                return widget[touchEndedCallBackName](touch, event);
        }
        var onTouchCancelled = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchCancelledCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchCancelledCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchCancelledCallBackName] === "function")
                return widget[touchCancelledCallBackName](touch, event);
        }

        if(touchSwallowEnabled) {
            touchSwallowEnabled = true;
        }
        else {
            touchSwallowEnabled = false;
        }

        if(this._oneByOneCallbackListener) {
            cc.eventManager.removeListener(this._oneByOneCallbackListener);
        }

        this._oneByOneCallbackListener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: touchSwallowEnabled,
            onTouchBegan: onTouchBegan,
            onTouchMoved: onTouchMoved,
            onTouchEnded: onTouchEnded,
            onTouchCancelled: onTouchCancelled
        });

        cc.eventManager.addListener(this._oneByOneCallbackListener, this);
    },

});